package com.project.controller.web;

import com.project.entity.User;
import com.project.myBeans.ResponseBean;
import com.project.service.UserService;
import com.project.service.base.BaseService;
import com.project.service.base.CrpData.CrypDataHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.project.myBeans.ResponseBean;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by WheelChen on 2017/5/7.
 * <p>
 * 用户操作
 */
@Controller
@RequestMapping("/user")
public class UserController extends BaseService {

    @Autowired
    UserService userService;

    /**
     * author: WheelChen
     * date: 2017-4-25
     * function: 修改用户密码
     *
     * @param user
     * @return 返回json类型的user
     */
    @RequestMapping(value = "/modifyPwd", method = RequestMethod.POST)
    @ResponseBody
    public ResponseBean modifyPwd(@RequestBody User user) {
        try {
            User oldUser = userService.findOneAllByUserID(user.getId());
            if (!userService.validatePassword(oldUser, user.getPassword())) { //oldPassword:user.getPassword()
                responseBean.setMessage("原密码不正确");
                responseBean.setSuccess(false);
            } else {
                String newPassword = user.getNewPassword();
                String CheckNewPassword = user.getCheckNewPassword();
                if (!newPassword.equals(CheckNewPassword)) {
                    responseBean.setMessage("新密码两次输入不相同");
                    responseBean.setSuccess(false);
                } else {
                    user.setPassword(user.getNewPassword());
                    userService.updateUser(user);
                    responseBean.setMessage("修改成功");
                    responseBean.setSuccess(true);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            responseBean.setSuccess(false);
            responseBean.setMessage("更新失败");
        }
        return responseBean;
    }
    
    @RequestMapping(value = "/resetPassword", method = RequestMethod.POST)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/9/15
     * function: 重置密码
     *
     * @param user
     * @return
     */
    public ResponseBean resetPassword(@RequestBody User user){
        ResponseBean responseBean = new ResponseBean();
        try {
            user.setPassword(user.getPassword());
            userService.updateUser(user);
        } catch (Exception e) {
            e.printStackTrace();
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;
    }
    

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    /**
     * author: WheelChen
     * date: 2017/6/26
     * function: 注册用户
     *
     * @param user
     * @return
     */
    public String registerUser(User user) throws Exception {
        if (!userService.checkUsernameIsExist(user.getUsername()))
            userService.createUser(user);
        return "/userLogin";
    }

    @RequestMapping(value = "/checkUserName/{username}", method = RequestMethod.GET)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/8/8
     * function: 检查用户名是否已存在
     *
     * @param username 用户名
     * @return true - 存在 false - 不存在
     */
    public ResponseBean checkUserName(@PathVariable("username") String username) {
        ResponseBean responseBean = new ResponseBean();
        try {
            System.out.println("user name===" + username);

            if (userService.checkUsernameIsExist(username)) {
                responseBean.setSuccess(true);//存在
            } else {
                responseBean.setSuccess(false);
            }
        } catch (Exception e) {
            e.printStackTrace();
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;

    }

    @RequestMapping(value = "/findOneById/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseBean findOneById(@PathVariable("id") Integer id) {
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.setData(userService.findOneByUserID(id));
            responseBean.setMessage("获取成功");
            responseBean.setSuccess(true);
        } catch (Exception e) {
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;
    }

    @RequestMapping(value = "/modifyOne", method = RequestMethod.POST)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/8/1
     * function: 修改用户表信息
     *
     * @param user
     * @return
     */
    public ResponseBean modifyOne(@RequestBody User user) {
        ResponseBean responseBean = new ResponseBean();
        try {
            userService.updateUser(user);
            responseBean.setSuccess(true);
            responseBean.setMessage("修改成功");
        } catch (Exception e) {
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;
    }


}
